<?php

namespace app\service\impl;

use app\service\AlbulmsService;
use fast\Http;
use think\facade\Log;
use Swoole\Process;

/**
 * 洞签平台
 */
class AlbumsLocalServiceImpl implements AlbulmsService{

    protected $config;

    public function __construct(){
        $service_type=config('capture.platform');
        $this->config=config('capture.'.$service_type);
    }

    public function sys_lists() {

    }

    public function create_album($data){

    }

    public function edit_album($data) {

    }

    public function del_album($data){

    }

    public function create_face($data,$album_type=1,$album_id=''){

    }

    public function compare_face($photo,$compare_type=1,$start_time='',$end_time=''){


        // 定义子进程要执行的函数
        $compare = function ($taskId, $data) {
            echo "Task ID: {$taskId}, Data: {$data}\n";
            // 执行具体的任务逻辑
            sleep(1); // 模拟耗时操作
            return "Result of task {$taskId}";
        };

        // 创建子进程并分配任务
        $processes = [];
        foreach ($tasks as $index => $taskId) {
            $processes[] = new Process(function ($worker, $id) use ($taskId, $compare) {
                $worker->name("TaskWorker_{$id}");
                echo "Process {$id} is starting with task ID {$taskId}\n";
                $result = $taskFunction($taskId, "Data for task {$taskId}");
                $worker->exit($result); // 将结果传递给父进程
            }, false, 0, $index + 1); // 第四个参数是进程的 ID
        }

        // 启动所有进程
        foreach ($processes as $process) {
            $pid = $process->start();
            echo "Process with PID {$pid} started\n";
        }

        // 收集进程结果
        foreach ($processes as $index => $process) {
            $pid = $process->pid;
            $result = $process->read(); // 读取子进程发送的结果
            echo "Result from PID {$pid}: {$result}\n";
            unset($processes[$index]); // 清理进程对象
        }

        // 等待所有进程结束
        foreach ($processes as $process) {
            $process->close();
        }
    }

    private function compute($arg) {

    }
}